package com.zslc.securitymonitoring.service.impl;

import com.zslc.securitymonitoring.dao.*;
import com.zslc.securitymonitoring.enums.AlermStatusEnum;
import com.zslc.securitymonitoring.service.RealTimeService;
import com.zslc.securitymonitoring.util.ResultCMS;
import com.zslc.securitymonitoring.util.StringUtils;
import com.zslc.securitymonitoring.vo.GlobalSeverityVO;
import com.zslc.securitymonitoring.vo.StatusStatistic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

/**
 * @version 1.0
 * @description:
 * @author: Gaoyc
 * @date: 2024-06-12 18:19
 **/
@Service
public class RealTimeServiceImpl implements RealTimeService {
    @Autowired
    private AvailabilityMapper availabilityMapper;
    @Autowired
    private PhishingMapper phishingMapper;
    @Autowired
    private SpamlinkMapper spamlinkMapper;
    @Autowired
    private TrojanMapper trojanMapper;
    @Autowired
    private VulMapper vulMapper;
    @Autowired
    private AlertLogMapper alertLogMapper;
    @Qualifier("asyncDataExecutor")
    @Autowired
    private TaskExecutor taskExecutor;

    @Override
    public List<GlobalSeverityVO> listBy1Day() throws ExecutionException, InterruptedException {
        //异步执行5个独立的查库操作
        CompletableFuture<List<GlobalSeverityVO>> avaCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return availabilityMapper.listBy1Day();
        },taskExecutor);
        CompletableFuture<List<GlobalSeverityVO>> phishCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return phishingMapper.listBy1Day();
        },taskExecutor);
        CompletableFuture<List<GlobalSeverityVO>> spamlinkCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return spamlinkMapper.listBy1Day();
        },taskExecutor);
        CompletableFuture<List<GlobalSeverityVO>> trojanCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return trojanMapper.listBy1Day();
        },taskExecutor);
        CompletableFuture<List<GlobalSeverityVO>> vulCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return vulMapper.listBy1Day();
        },taskExecutor);

        List<GlobalSeverityVO> avalList = avaCompletableFuture.get();
        List<GlobalSeverityVO> phisList = phishCompletableFuture.get();
        List<GlobalSeverityVO> spamList = spamlinkCompletableFuture.get();
        List<GlobalSeverityVO> trojList = trojanCompletableFuture.get();
        List<GlobalSeverityVO> vulList = vulCompletableFuture.get();
        avalList.addAll(phisList);
        avalList.addAll(spamList);
        avalList.addAll(trojList);
        avalList.addAll(vulList);
        List<GlobalSeverityVO> resultList = avalList.stream().sorted(Comparator.comparing(GlobalSeverityVO::getUpdatedAt, Comparator.reverseOrder()))
                .collect(Collectors.toList());
        return resultList;
    }

    @Override
    public List<StatusStatistic> statusStatistic() throws ExecutionException, InterruptedException {
        //异步执行5个独立的查库操作
        CompletableFuture<List<StatusStatistic>> avaCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return availabilityMapper.statusStatistic();
        },taskExecutor);
        CompletableFuture<List<StatusStatistic>> phishCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return phishingMapper.statusStatistic();
        },taskExecutor);
        CompletableFuture<List<StatusStatistic>> spamlinkCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return spamlinkMapper.statusStatistic();
        },taskExecutor);
        CompletableFuture<List<StatusStatistic>> trojanCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return trojanMapper.statusStatistic();
        },taskExecutor);
        CompletableFuture<List<StatusStatistic>> vulCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return vulMapper.statusStatistic();
        },taskExecutor);
        CompletableFuture<List<StatusStatistic>> logCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return alertLogMapper.confirmedStatisticBy30Day();
        },taskExecutor);

        List<StatusStatistic> avalList = avaCompletableFuture.get();
        List<StatusStatistic> phisList = phishCompletableFuture.get();
        List<StatusStatistic> spamList = spamlinkCompletableFuture.get();
        List<StatusStatistic> trojList = trojanCompletableFuture.get();
        List<StatusStatistic> vulList = vulCompletableFuture.get();
        avalList.addAll(phisList);
        avalList.addAll(spamList);
        avalList.addAll(trojList);
        avalList.addAll(vulList);

        List<StatusStatistic> resultList = new ArrayList<>();
        for (AlermStatusEnum statusEnum : AlermStatusEnum.values()) {
            StatusStatistic statusStatistic = new StatusStatistic();
            Integer num = avalList.parallelStream().filter(a ->
                            StringUtils.equalsIgnoreCase(statusEnum.getValue(), a.getStatus()))
                    .collect(Collectors.summingInt(StatusStatistic::getNum));
            statusStatistic.setNum(num);
            statusStatistic.setStatus(statusEnum.getValue());
            resultList.add(statusStatistic);
        }

        //30天已处理报警数量
        List<StatusStatistic> confirmedStatisticList = logCompletableFuture.get();
        Integer confirmedNum = confirmedStatisticList.stream().collect(Collectors.summingInt(StatusStatistic::getNum));
        StatusStatistic statusStatistic = new StatusStatistic();
        statusStatistic.setStatus("deal");
        statusStatistic.setNum(confirmedNum);
        resultList.add(statusStatistic);
        return resultList;
    }

}
